package orbitPrototype.game;

import java.awt.event.MouseEvent;
import java.util.ArrayList;

import orbitPrototype.Settings;
import orbitPrototype.Util;
import orbitPrototype.event.EventHandler;
import orbitPrototype.gui.GUIWindow;
import orbitPrototype.gui.GUIWindowManager;
import orbitPrototype.input.MouseInput;
import orbitPrototype.loaders.XMLLoader;
import orbitPrototype.objects.DisplayObject;
import orbitPrototype.objects.DisplayObjectContainer;
import orbitPrototype.objects.TiledSprite;

public class Game implements Runnable
{
	private long _tickStartTime;
	private long _tickCurrent;
	private int _tickMax = 5;

	private DisplayObjectContainer _mainDisplayObjectContainer = null;
	private GUIWindowManager _guiWindowManager = null;
	
	private MouseInput _mouseInput = null;
	
	private GUIWindow _mainMenu = null;
	
	public Game()
	{
		_mainDisplayObjectContainer = new DisplayObjectContainer(Settings.FRAME_WIDTH, Settings.FRAME_HEIGHT);
		_guiWindowManager = new GUIWindowManager();
		
		initInput();
		initMenu();
		//initEventListeners();
		
		//addTestSprite();
	}
	
	private void addTestSprite()
	{
		_mainDisplayObjectContainer.addChild(new TiledSprite(0, 0, 175, 356, "gui", "default_window_header_side_r"));
	}
		
	private void initInput()
	{
		_mouseInput = new MouseInput(this);
	}
	
	private void initMenu()
	{
		_mainDisplayObjectContainer.addChild(_guiWindowManager);
		
		_mainMenu = new GUIWindow(0, 0, XMLLoader.getXMLElementByUniqueName("NewFile", "mainMenu"));
		_guiWindowManager.addChild(_mainMenu);
		
		_guiWindowManager.addChild(new GUIWindow(0, 0, XMLLoader.getXMLElementByUniqueName("NewFile", "testWindow")));
	}
	
	private void initEventListeners()
	{
		EventHandler.registerEventListener("startGame", this, "startGame", false);
	}
	
	private void startGame()
	{
		_guiWindowManager.removeChild(_mainMenu);
	}
				
	public void run()
	{
		while(true)
		{
			_tickStartTime = Util.getCurrentTick();
			
			/*
			 * START GAME LOGIC
			 */
			
			/*
			 * END GAME LOGIC
			 */
			
	        _tickCurrent = Util.getCurrentTick();
	        long tickTime = _tickCurrent -_tickStartTime;
	        long sleep = 1000 / _tickMax - tickTime;
	        if(sleep > 0)
	        {
	        	try 
	        	{
					Thread.sleep(sleep);
				} 
	        	catch (InterruptedException e) 
	        	{
					e.printStackTrace();
				}				
			}
		}
	}
	
	
	/*
	 * Temp testing function function
	 */
	public void mouseDownTest(int x, int y)
	{
		Util.output("Clicked: " + x + ", " + y);
		
		ArrayList<DisplayObject> clickedList = _mainDisplayObjectContainer.getDisplayObjectsUnderPoint(x, y);
		if(clickedList.size() > 0)
		{
			Util.output("Objects under click: " + clickedList.size());
			for(DisplayObject clicked:clickedList)
			{
				Util.output("... " + clicked.getClass());
			}
		}
	}
	
	public void mouseClick(int x, int y, int button)
	{
		if(button == MouseEvent.BUTTON1)
		{
			mouseDownTest(x, y);
			//_guiWindowManager.primaryMouseClick(x, y);
		}
		
		
		// INSERT HERE
	}
	
	public void mouseDown(int x, int y, int button)
	{
		_guiWindowManager.mouseDown(x, y, button);
	}
	
	public void mouseUp(int x, int y, int button)
	{
		_guiWindowManager.mouseUp(x, y, button);
	}
	
	public void primaryMouseDrag(int x, int y, boolean start)
	{
		_guiWindowManager.primaryMouseDrag(x, y, start);
	}
	
	public void mouseMove(int x, int y)
	{
		_guiWindowManager.mouseMoved(x, y);
	}
	
	/*
	 * GETTERS
	 */
	
	public DisplayObjectContainer getMainDisplayObjectContainer()
	{
		return _mainDisplayObjectContainer;
	}
	
	public MouseInput getMouseInput()
	{
		return _mouseInput;
	}
}
